package com.sangmo.fts.process.storage.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.plugin.dataview.crud.service.DelegateViewService;
import com.sangmo.boot.framework.plugin.dataview.crud.service.ViewService;
import com.sangmo.fts.process.storage.model.CsfAgreement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CsfAgreementService extends DelegateViewService<CsfAgreement> {

    @Autowired
    private CsfAgreementEntityService csfAgreementEntityService;

    @Override
    protected ViewService<CsfAgreement> getEntityService() {
        return csfAgreementEntityService;
    }

    public void buyerConfirm(Long id) {
        CsfAgreement agreement = this.get(id);
        if (null == agreement.getBuyerConfirmDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.buyerConfirm.permit.disabled");
        }
        if (agreement.isBuyerConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.buyerConfirm.state.wrong");
        }
        csfAgreementEntityService.buyerConfirm(agreement);
    }

    public void sellerConfirm(Long id) {
        CsfAgreement agreement = this.get(id);
        if (null == agreement.getSellerConfirmDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.sellerConfirm.permit.disabled");
        }
        if (agreement.isSellerConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.sellerConfirm.state.wrong");
        }
        csfAgreementEntityService.sellerConfirm(agreement);
    }

    public void bankConfirm(Long id) {
        CsfAgreement agreement = this.get(id);
        if (null == agreement.getBankConfirmDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.bankConfirm.permit.disabled");
        }
        if (agreement.isBankConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.bankConfirm.state.wrong");
        }
        csfAgreementEntityService.bankConfirm(agreement);
    }

    public void storageConfirm(Long id) {
        CsfAgreement agreement = this.get(id);
        if (null == agreement.getStorageConfirmDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.storageConfirm.permit.disabled");
        }
        if (agreement.isStorageConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.csfAgreement.storageConfirm.state.wrong");
        }
        csfAgreementEntityService.storageConfirm(agreement);
    }

    public CsfAgreement findByContractId(Long contractId) {
        return csfAgreementEntityService.findByContractId(contractId);
    }



}
